package org.syndicate.web;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.syndicate.domain.Loan;

@RooWebScaffold(path = "loans", formBackingObject = Loan.class)
@RequestMapping("/loans")
@Controller
public class LoanController {
	
	@RequestMapping(method = RequestMethod.POST)
    public String create(@Valid Loan loan, BindingResult result, Model model, HttpServletRequest request) {
        if (result.hasErrors()) {
            model.addAttribute("loan", loan);
            addDateTimeFormatPatterns(model);
            return "loans/create";
        }
        loan.create();
        return "redirect:/loans/" + encodeUrlPathSegment(loan.getId().toString(), request);
    }
}
